<?php

namespace Biz\service\advertisement\apiModule;


use Doraemon\service\Photos;
use Doraemon\service\statSign\Basic;
use Doraemon\tools\BlockType;
use Doraemon\tools\BlockTypeMapping;
use Doraemon\tools\HhzClient;
use Doraemon\tools\ImgUrl;
use Hhzbeads\Img\ImageType;

class SingleBanner extends BaseModule
{

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {
        $upgradeVersion = '5.21.0';
        $banner = [];
        if ($hit) {
            $type = $hit['position_id'];
            $link = $this->generateLink($hit['link_type'], $hit['link_params']);
            if ($this->position_id == 115) {
                $objRs = BlockTypeMapping::getObjFromLinkParams($hit['link_type'], $hit['link_params']);
                $obj_id = $objRs['obj_id'] ?? null;
                $statSignExt = [
                    Basic::EXTEND_KEY_CONFIG_ID => $hit['id'],
                    Basic::EXTEND_KEY_URL => $link,
                    Basic::EXTEND_KEY_REASON_TYPE => \Biz\model\advertisement\MPositionBaseInfo::REASON_SIGN_MAP[$hit['reason']] ?? '',
                ];
                if ($obj_id) $statSignExt[Basic::EXTEND_KEY_OBJ_ID] = $obj_id;
            }
            if ($this->position_id == 116) {
                $statSignExt = [
                    Basic::EXTEND_KEY_CONFIG_ID => $hit['id'],
                    Basic::EXTEND_KEY_POS => 'top_left',
                ];
            }
            if ($this->position_id == 117) {
                $statSignExt = [
                    Basic::EXTEND_KEY_CONFIG_ID => $hit['id'],
                    Basic::EXTEND_KEY_POS => 'center',
                ];
            }
            $statSign = $this->statSign($hit, $statSignExt);
            $url = '';
            if ($this->position_id > 100) {
                $extend = [];
                if ($this->position_id == self::HOME_TOP_TRIFLE_BANNER || $this->position_id == 116 || $this->position_id == 117) $extend = ['play_times' => (int)$hit['play_times']];
                if (in_array($this->position_id, [self::PUBLISH_BTN, self::TAB_NAV_BG])) return strlen($hit['pic_id']) ? ImgUrl::getUrl(ImageType::APPTYPE_OP, $hit['pic_id'], ImageType::IMAGE_SIZE_OP_ORI) : '';
                return [
                    'type' => BlockType::TYPE_BANNER,
                    'banner' => [
                        'id' => $hit['id'],
                        'banner' => strlen($hit['pic_id']) ? ImgUrl::getUrl(ImageType::APPTYPE_OP, $hit['pic_id'], ImageType::IMAGE_SIZE_OP_ORI) : '',
                        'link' => $link,
                    ] + $extend,
                    'statSign' => $statSign,
                ];
            }
            switch ($type) {
                case self::HOME_RANKING:
                    $banner = [
                        'title' => $hit['title'],
                        'rank_id' => $hit['ext_type'],
                    ];
                    //todo 替换成榜单内容
                    break;
                case self::HOME_ROOF:
                    $link_params = json_decode($hit['link_params'], true);
                    $url = empty($link_params['url']) ? '' : $link_params['url'];
                case self::POINTS_MALL_BANNER:
                    $banner = [
                        'type' => BlockType::TYPE_BANNER,
                        'banner' => [
                            'id' => $hit['id'],
                            'banner' => strlen($hit['pic_id']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '',
                            'link' => $url ?: $link,
                            'statSign' => $statSign,
                        ],
                    ];
                    break;
                case self::HOME_TODAY_ARTICLE:
                    $ext = $hit['ext_type'] > 0;
                    $banner = [
                        'type' => BlockType::TYPE_BANNER,
                        'banner' => [
                            'id' => $hit['id'],
                            'title' => $ext ? '今日小事' : '今日整屋',
                            'cover_pic_color' => '#000000', //取色值
                            'subtitle' => $hit['title'],
                            'date' => date("n.j"),
                            'banner' => strlen($hit['pic_id']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '',
                            'link' => $this->generateLink($hit['link_type'], $hit['link_params']),
                            'statSign' => $statSign,
                        ],
                    ];
                    if (version_compare(HhzClient::getClientVersion(), '6.0.0', '>=')) {
                        $banner['banner']['statSign'] = Basic::genStatSign('home-today_article', [
                            Basic::EXTEND_KEY_CONFIG_ID => $hit['id'],
                            Basic::EXTEND_KEY_OBJ_TYPE => 1115,
                        ]);
                    }
                    if (version_compare(HhzClient::getClientVersion(), $upgradeVersion, '>=')) {
                        $banner = $banner['banner'];
                    }
                    $banner = \Doraemon\tools\StatSign::versionCompatible($banner, $upgradeVersion, BlockType::TYPE_BANNER, 'banner', false);

                    break;
                case self::NOTE_DETAIL_RECOMMEND_BANNER:
                    $objRs = BlockTypeMapping::getObjFromLinkParams($hit['link_type'], $hit['link_params']);
                    $obj_id = $objRs['obj_id'] ?? null;
                    $obj_type = $objRs['obj_type'] ?? null;
                    $photo_list = Photos::getPhotosForDynamic([$obj_id], $uid);
                    if ($photo_list && isset($photo_list[$obj_id])) {
                        $tempData = [
                            'photo' => $photo_list[$obj_id],
                        ];
                    }
                    if (empty($tempData)) break;
                    $banner = [
                        'type' => BlockType::TYPE_PHOTO,
                        'index' => intval($hit['pos']),
                        'statSign' => $this->statSign($hit, [Basic::EXTEND_KEY_OBJ_ID => $obj_id, Basic::EXTEND_KEY_INDEX => $hit['pos']]),
                    ] + $tempData;
                    break;
                case self::HOME_RECOMMEND_GREET_BANNER:
                case self::TAB5_GREET_BANNER:
                case self::TAB3_TOP_BANNER:
                case self::TOPIC_TOP_BANNER:
                    $banner[] = [
                        'id' => $hit['id'],
                        'pos' => $hit['pos'],
                        'link' => $link,
                        'adm_comment' => $hit['remark'],
                        'pic_id' => $hit['pic_id'],
                        'statSign' => $statSign,
                    ];
                    break;
                case self::TBMINI_HOME_ACTIVITY:

                    $banner = [
                        'tbmini_text' => json_decode($hit['ext']['tbmini_text'],true) ?? [],
                    ];
                    break;
                default:
                    $banner = [
                        'id' => intval($hit['id']),
                        'banner' => strlen($hit['pic_id']) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $hit['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI) : '',
                        'link' => $link,
                        'pic_id' => $hit['pic_id'],
                        'adm_comment' => $hit['remark'],
                        'statSign' => $statSign,
                    ];
                    break;
            }
        }
        return $banner;
    }

}